package com.nami.framework.security.handler;

import com.nami.common.response.R;
import com.nami.common.utils.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义用户注销成功处理类 Handler
 *
 * @author C
 */
@Component
@Slf4j
public class SecurityLogoutSuccessHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
        SecurityContextHolder.clearContext();
        log.info("用户注销成功");
        if (ServletUtils.isAjax(request)) {
            ServletUtils.writeJson(R.ok("注销成功"));
        } else {
            response.sendRedirect("/login");
        }

    }
}
